package com.wiegand.loader {
	import com.wiegand.error.ErrorHandler;
	import com.wiegand.event.LoaderEvent;
	import com.wiegand.loader.XmlLoader;

	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;

	/**
	 * @author kwiegand
	 */
	 
	public class XmlLoader extends EventDispatcher{

		private static var _instance : XmlLoader;

		private var _files : 		Array;						
		private var _data : 		Array;		
		private var _loader : 		URLLoader;		
		private var _i : 			int = 0;

		public static function getInstance() : XmlLoader{
			if( _instance == null )
				_instance = new XmlLoader( new SingletonEnforecer() );
			return _instance;
		}
		
		public function XmlLoader( blocker:SingletonEnforecer ):void{
			if ( blocker == null ) {
            	throw new Error("Error: Instantiation failed: Use XmlLoader.getInstance() instead of new.");
          	}
          	else{
          		init();
          	}
		}
		
		private function init() : void{
		
			_data = new Array();
			
			_loader = new URLLoader();
			_loader.dataFormat = URLLoaderDataFormat.TEXT;
			
			_loader.addEventListener( Event.COMPLETE, handleComplete );
			_loader.addEventListener( ErrorEvent.ERROR, errorHandler);
			_loader.addEventListener( IOErrorEvent.IO_ERROR, errorHandler);
		}
		
		
		private function errorHandler( e: ErrorEvent) :void{
				
			ErrorHandler.getInstance().show(1);
			
		}
		
		
		public function load( files : Array ):void{
			
			_i = 0;
			
			_data = new Array();
		
			_files = files;
						
			dispatchEvent( new LoaderEvent( LoaderEvent.EVENT_START_LOADING ) );	
			
			loadFile();
			
		}
		
		private function loadFile() : void{
			
			
			if( _i < _files.length ){
				
				
				try{
					_loader.load( new URLRequest( _files[ _i ] ) );
				}
				catch ( e : TypeError){
					ErrorHandler.getInstance().show(1);
				}
				
			}
			else{
				
				_i = 0;
				
				dispatchEvent( new LoaderEvent( LoaderEvent.EVENT_LOADING_COMPLETE ));	

			}
		
		}
		
		private function handleComplete( event : Event ) : void {
			
			try{
				
				var xml : XML = XML( event.target[ "data" ] );
				
				_data.push( xml );
			
				_i++;
				
				loadFile();
				
			}
			catch ( e : TypeError){
				
				ErrorHandler.getInstance().show(1);
				
			}
			
		}
		
		public function getXmlData() : Array{
			
			return _data;
			
		}
		
		public function getXml( n : int ) : XML{
			
			return _data[ n ] as XML;
			
		}
		
		public function clear():void{
			
			for( var i : int = 0; i < _files.length; i++){
				_files[i] = null;
			}
			_files = null;

			for( var j : int = 0; j < _data.length; j++){
				_data[j] = null;
			}
			
			_data = null;

		
		}
	}
}

internal class SingletonEnforecer {}
